#include "LED.h"
#include"KEY.h"
#include <string.h>
#include "PWM.h"
#include "UART.h"

#if 0
uint8 RLedBuf;
uint8 CntLedFlash;
volatile uint16 TimeLedOn;
// const uint8 TAB_TYPE_SEL[] = {0x01,0x03,0x07};
bit F_LED_SEND;
uint8 CntFlashTImes;

const uint8 TAB_LED[] = {0x00,0x01,0x02,0x04,0x08};
void    initLED(void)
{
    DIS_LED1();
    DIS_LED2();
    DIS_LED3();
    DIS_LED4();
    DIS_LED5();

    TimeLedOn = 0;
    CntLedFlash = 0;
    F_LED_SEND = 0;
    CntFlashTImes = 0;
}

void    SetLed(uint8 led_buf,uint16 time_on)
{
    TimeLedOn = time_on;

    RLedBuf = led_buf;
    CntLedFlash = 0;

    // CLOSE_LED_COM();

    F_LED_SEND = 1;
    if(time_on)
        CntFlashTImes = 6;
}

void    Led_Main(void)
{    
    if(RLedBuf&0x01)    EN_LED1();  
    else                DIS_LED1(); 
    if(RLedBuf&0x02)    EN_LED2();  
    else                DIS_LED2();
    if(RLedBuf&0x04)    EN_LED3();  
    else                DIS_LED3(); 
    if(RLedBuf&0x08)    EN_LED4();  
    else                DIS_LED4(); 
    if(RLedBuf&0x10)    EN_LED5();  
    else                DIS_LED5(); 

    if((RLedBuf != ALL_ON) && (RLedBuf != ALL_OFF))
    {
        // if(F_LED_SEND)      EN_LED5();      
        // else                DIS_LED5();     
        CntLedFlash++;
        if(CntLedFlash >= 12)
        {
            CntLedFlash = 0;
            if(CntFlashTImes)   
            {
                CntFlashTImes--;
                F_LED_SEND = ~F_LED_SEND;
            }
            if(CntFlashTImes == 0)
                F_LED_SEND = 0;
        }

        // if(F_LED_SEND)  RLedBuf |= 0x10;
        // else            RLedBuf &= ~0x10;
        if(F_LED_SEND)      EN_LED5();      
        else                DIS_LED5();    
    }
    else
    {  
        CntLedFlash = 0;
        F_LED_SEND = 1;    
        if(RLedBuf&0x10)    EN_LED5();      
        else                DIS_LED5(); 
    }
    
    //---- LED 处理 ----//
    if(TimeLedOn)   {TimeLedOn--;}
    else
    {         
        DIS_LED1();
        DIS_LED2();
        DIS_LED3();
        DIS_LED4();
        DIS_LED5();
    }
    //------------------//
}
#endif

Typedef_LED LedCtrl = {0};

void    initLED(void)
{
    P_LED_COM0 = LOW;
    P_LED_COM1 = LOW;
    P_LED_COM2 = LOW;
    DIS_LED1();
    DIS_LED2();
    DIS_LED3();
    DIS_LED4();
    DIS_LED5();
    memset(&LedCtrl,0,sizeof(LedCtrl));
}

void LED_Set(uint8 buf, uint8 TopOrBottom, uint16 tm_on, uint16 tm_period, uint8 loop,void(*FuncTimerCallBack)(void))
{
    LedCtrl.Cnt = 0;
    LedCtrl.Buf = buf;
    LedCtrl.TopOrBottom = TopOrBottom;
    LedCtrl.TmOn = tm_on;
    LedCtrl.TmPeriod = tm_period;
    LedCtrl.Loop = loop;
    LedCtrl.FuncCallBack = FuncTimerCallBack;
}

void LED_Main(void)
{
    if(LedCtrl.Loop)
    {
        LedCtrl.Cnt++;
        if(LedCtrl.Cnt >= LedCtrl.TmPeriod)
        {
            LedCtrl.Cnt = 0;
            if(LedCtrl.Loop != INFINITE)
            {
                LedCtrl.Loop--;
                if((LedCtrl.Loop == 0) && (LedCtrl.FuncCallBack != NULL))
                {
                    LedCtrl.FuncCallBack();
                }
            }
        }
        
        if((LedCtrl.Cnt >= LedCtrl.TmOn) || (LedCtrl.Loop==0))
        {
            LED_ALL_OFF();
        }
        else
        {
            if((LedCtrl.TopOrBottom & TOP) == TOP)
            {
                // SET_OUT(LED_COM1,LOW);
                if(LedCtrl.Buf & LED1)  EN_LED1();
                else                    DIS_LED1();
                if(LedCtrl.Buf & LED2)  EN_LED2();
                else                    DIS_LED2();
                if(LedCtrl.Buf & LED3)  EN_LED3();
                else                    DIS_LED3();
                if(LedCtrl.Buf & LED4)  EN_LED4();
                else                    DIS_LED4();
                if(LedCtrl.Buf & LED5)  EN_LED5();
                else                    DIS_LED5();
                SET_OUT(LED_COM0,HIGH);
            }
            if((LedCtrl.TopOrBottom & BOTTOM) == BOTTOM)
            {
                // SET_OUT(LED_COM0,LOW);
                if(LedCtrl.Buf & LED5)  EN_LED1();
                else                    DIS_LED1();
                if(LedCtrl.Buf & LED4)  EN_LED2();
                else                    DIS_LED2();
                if(LedCtrl.Buf & LED3)  EN_LED3();
                else                    DIS_LED3();
                if(LedCtrl.Buf & LED2)  EN_LED4();
                else                    DIS_LED4();
                if(LedCtrl.Buf & LED1)  EN_LED5();
                else                    DIS_LED5();
                SET_OUT(LED_COM1,HIGH);
            }
        }
    }
    else
    {
        LED_ALL_OFF();
    }
}

uint8 LED_Check(void)
{
    return LedCtrl.Loop;
}


